﻿using System;
using System.Windows.Forms;
using FastFood.Business.Entities;
using FastFood.Business.Logic;
using FastFood.Windows.Helpers;


namespace FastFood.Windows.Forms.Administracion.Comestibles
{
    //public delegate void GetObjeto(object o );

    public partial class CwfPreparablesBusqueda : UserControl
    {
        PreparablesManager preparableManager;
        public event GetObjeto CwfPreparablesBusqueda_SeleccionEntidad;

        public CwfPreparablesBusqueda()
        {
            InitializeComponent();
            preparableManager = new PreparablesManager();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            buscarVendible();
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            limpiar();
        }

        private void insumosDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                if (CwfPreparablesBusqueda_SeleccionEntidad != null && preparablesBindingSource.Current != null)
                    CwfPreparablesBusqueda_SeleccionEntidad(preparablesBindingSource.Current);
            }

        }

        private void buscarVendible()
        {
            preparablesBindingSource.DataSource = preparableManager.ListarByNombre(txtNombre.Text);
        }

        public void limpiar()
        {
            txtNombre.Text = string.Empty;
            preparablesBindingSource.Clear();
            txtNombre.Focus();
        }

        private void txtNombre_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (preparablesBindingSource.Count > 0)
                {
                    preparablesDataGridView.Focus();
                    preparablesBindingSource.MoveFirst();
                }
            }

            else if (e.KeyCode == Keys.Enter)
            {
                if (preparablesBindingSource.Count == 1)
                {
                    CwfPreparablesBusqueda_SeleccionEntidad(preparablesBindingSource.Current);
                }
            }
        }

        private void CwfInsumoBusqueda_Load(object sender, EventArgs e)
        {
            txtNombre.Focus();
        }

        private void insumosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (CwfPreparablesBusqueda_SeleccionEntidad != null && preparablesBindingSource.Current != null)
                CwfPreparablesBusqueda_SeleccionEntidad(preparablesBindingSource.Current);
        }

        private void txtNombre_TextChanged(object sender, EventArgs e)
        {
            buscarVendible();
        }
    }
}
